//
//  FPMyCollectMovieViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/24.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPMyCollectMovieViewController.h"
#import "FPSqliteManager.h"

#import "FPCollectMovieModel.h"
#import "FPmainTopViewModel.h"
#import "FPMovieDetailViewController.h"
@interface FPMyCollectMovieViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic , strong)UITableView *tableView;
@property (nonatomic , strong)NSMutableArray *movieArr;
@end

@implementation FPMyCollectMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self setSQLValue];
    
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    UIView *statusView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    statusView.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    [self.view addSubview:statusView];
}

- (void)setSQLValue
{
   NSArray *arry = [FPSqliteManager selectMovieWithUserName:[FPUserModel ShareInstance].userName];
    if (arry.count > 0) {
        
        self.movieArr = [NSMutableArray array];
    }
    for (NSString *movieID in arry) {
        FPmainTopViewModel *model = [FPSqliteManager selectModelWithMovieID:movieID];
        [_movieArr addObject:model];
    }
    
    
    
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _movieArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    FPmainTopViewModel *model = _movieArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:model.imgstr];
    [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"296b467c57933f291a29e2c2255af022.jpg"]];
    cell.textLabel.text = model.titleCn;
    cell.detailTextLabel.text = model.titleEn;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FPmainTopViewModel *model = _movieArr[indexPath.row];
    FPMovieDetailViewController *movieDetailVC = [[FPMovieDetailViewController alloc]init];
    movieDetailVC.viewModel = model;
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    
    
    
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    FPmainTopViewModel *model = _movieArr[indexPath.row];
    [FPSqliteManager deleteDataWithMovieID:model.movieID];
    [_movieArr removeObjectAtIndex:indexPath.row];
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
